//
//  HMStatusViewModel.swift
//  Weibo-swift
//
//  Created by GD on 16/9/23.
//  Copyright © 2016年 geduo. All rights reserved.
//

import UIKit
//封装微博首页 自定义cell 业务逻辑处理   自定义cell(首页cell)
class HMStatusViewModel: NSObject {
    //微博模型对象
    var status : HMStatus?
    
    //评论数
    var commentCountContent: String?
    
    //
    var retweetCountContent: String?
    
    //
    var unlikeCountContent :String?
    
    //  转发微博内容
    var retweetContent: String?
    
    
    //提供会员等级图片
    var mbrankImage : UIImage?
    
    
    init(status : HMStatus) {
        super.init()
        self.status = status
        
        //处理业务逻辑
        handleMBrankImage(mbrank: status.user?.mbrank ?? 0)
        
        retweetCountContent = handleCountContent(count: status.reposts_count, defaultTitle: "转发")
        
        commentCountContent = handleCountContent(count: status.comments_count, defaultTitle: "评论")
        unlikeCountContent = handleCountContent(count: status.attitudes_count, defaultTitle: "赞")
        handleRetweetContent(status: status)
    }
    
    //  处理转发微博内容显示
    private func handleRetweetContent(status: HMStatus) {
        
        if status.retweeted_status != nil {
            //  获取转发微博的用户名
            let name = status.retweeted_status?.user?.screen_name
            
            //  获取转发微博的内容
            let text = status.retweeted_status?.text
            
            if name != nil && text != nil {
                //  转发微博内容拼接
                retweetContent = "@\(name!): \(text!)"
                
            }
        }
    }

    
    
    //处理评论,转发,赞  数据显示
    private func handleCountContent(count:Int,defaultTitle:String) -> String{
        if count > 0 {
            return "\(count)"
        }else{
            return defaultTitle
        }
    }

    //  处理会员等级逻辑
    private func handleMBrankImage(mbrank:Int){
        //判断数据是否在1-6之间
        if mbrank >= 1 && mbrank <= 6 {
            mbrankImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
    }
    
}
